<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>集锦  &middot; sitename</title>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="viewport" content="width=device-width, initial-scale=1">


<meta name="description" content="集锦 description" />

<meta name="keywords" content="one, two, ">


<meta property="og:title" content="集锦  &middot; sitename ">
<meta property="og:site_name" content="sitename"/>
<meta property="og:url" content="https://lvzongcheng.gitee.io/java/%E9%9B%86%E9%94%A6/" />
<meta property="og:locale" content="en-EN">


<meta property="og:type" content="article" />
<meta property="og:description" content="集锦 description"/>
<meta property="og:article:published_time" content="2017-05-13T00:00:00Z" />
<meta property="og:article:modified_time" content="2017-05-13T00:00:00Z" />

  
    
<meta property="og:article:tag" content="one">
    
<meta property="og:article:tag" content="two">
    
  

  

<script type="application/ld+json">
  {
    "@context": "http://schema.org",
    "@type": "Article",
    "headline": "集锦",
    "author": {
      "@type": "Person",
      "name": "LvZongCheng"
    },
    "datePublished": "2017-05-13",
    "description": "集锦 description",
    "wordCount":  440 
  }
</script>



<link rel="canonical" href="https://lvzongcheng.gitee.io/java/%E9%9B%86%E9%94%A6/" />

<link rel="apple-touch-icon-precomposed" sizes="144x144" href="https://lvzongcheng.gitee.io/touch-icon-144-precomposed.png">
<link href="https://lvzongcheng.gitee.io/favicon.png" rel="icon">

<meta name="generator" content="Hugo 0.60.0" />

  <!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

<link href='https://fonts.googleapis.com/css?family=Merriweather:300%7CRaleway%7COpen+Sans' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="/css/font-awesome.min.css">
<link rel="stylesheet" href="/css/style.css">
<link rel="stylesheet" href="/css/highlight/default.css">

  
  
	<script>
	  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
	  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
	  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
	  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

	  ga('create', 'Your Google Analytics tracking code', 'auto');
	  ga('send', 'pageview');

	</script>

</head>
<body>
  <main id="main-wrapper" class="container main_wrapper has-sidebar">
    <header id="main-header" class="container main_header">
  <div class="container brand">
  <div class="container title h1-like">
  <a class="baselink" href="https://lvzongcheng.gitee.io">
  LvZongCheng

</a>

</div>

  
<div class="container topline">
  
  淡定 执著 进取 自律


</div>


</div>

  <nav class="container nav primary no-print">
  

<a class="homelink" href="https://lvzongcheng.gitee.io">home</a>


  
<a href="https://lvzongcheng.gitee.io/framework" title="Show list of Frameworks">Frameworks</a>

<a href="https://lvzongcheng.gitee.io/java" title="Show list of Java">Java</a>

<a href="https://lvzongcheng.gitee.io/linux" title="Show list of Linux">Linux</a>

<a href="https://lvzongcheng.gitee.io/redis" title="Show list of Redis">Redis</a>

<a href="https://lvzongcheng.gitee.io/sql" title="Show list of SQL">SQL</a>

<a href="https://lvzongcheng.gitee.io/Spring" title="Show list of Spring">Spring</a>

<a href="https://lvzongcheng.gitee.io/tools" title="Show list of Tools">Tools</a>

<a href="https://lvzongcheng.gitee.io/webpage" title="Show list of WebPage">WebPage</a>

<a href="https://lvzongcheng.gitee.io/information" title="Show list of information">information</a>

<a href="https://lvzongcheng.gitee.io/network" title="Show list of network">network</a>

<a href="https://lvzongcheng.gitee.io/%E5%BE%AE%E6%9C%8D%E5%8A%A1" title="Show list of 微服务">微服务</a>

<a href="https://lvzongcheng.gitee.io/%E6%80%BB%E7%BB%93" title="Show list of 总结">总结</a>

<a href="https://lvzongcheng.gitee.io/%E6%9C%BA%E5%99%A8%E8%87%AA%E5%8A%A8%E5%8C%96" title="Show list of 机器自动化">机器自动化</a>

<a href="https://lvzongcheng.gitee.io/%E6%B1%87%E7%BC%96" title="Show list of 汇编">汇编</a>

<a href="https://lvzongcheng.gitee.io/%E8%AE%B0%E4%BA%8B%E6%9C%AC" title="Show list of 记事本">记事本</a>


</nav>

<div class="container nav secondary no-print">
  
<a id="contact-link-email" class="contact_link" rel="me" aria-label="Email" href="mailto:Lyc19880405@163.com">
  <span class="fa fa-envelope-square"></span></a>



<a id="contact-link-github" class="contact_link" rel="me" aria-label="Github" href="https://github.com/enten/hugo-boilerplate">
  <span class="fa fa-github-square"></span></a>




 


















</div>


  

</header>


<article id="main-content" class="container main_content single">
  <header class="container hat">
  <h1>集锦
</h1>

</header>

  <div class="container content">
  <h3 id="java">Java获取文件路径的几种方法</h3>
<h6 id="heading">第一种：</h6>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">File f <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> File<span style="color:#f92672">(</span><span style="color:#66d9ef">this</span><span style="color:#f92672">.</span><span style="color:#a6e22e">getClass</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span><span style="color:#f92672">.</span><span style="color:#a6e22e">getResource</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;/&#34;</span><span style="color:#f92672">)</span><span style="color:#f92672">.</span><span style="color:#a6e22e">getPath</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span> 
System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span>f<span style="color:#f92672">)</span><span style="color:#f92672">;</span> 
</code></pre></div><p>结果: 
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin 
获取当前类的所在工程路径;</p>
<p>如果不加“/”</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">File f <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> File<span style="color:#f92672">(</span><span style="color:#66d9ef">this</span><span style="color:#f92672">.</span><span style="color:#a6e22e">getClass</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span><span style="color:#f92672">.</span><span style="color:#a6e22e">getResource</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;&#34;</span><span style="color:#f92672">)</span><span style="color:#f92672">.</span><span style="color:#a6e22e">getPath</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span> 
System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span>f<span style="color:#f92672">)</span><span style="color:#f92672">;</span> 
</code></pre></div><p>结果： 
C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test 
获取当前类的绝对路径；</p>
<h6 id="heading1">第二种：</h6>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">File directory <span style="color:#f92672">=</span> <span style="color:#66d9ef">new</span> File<span style="color:#f92672">(</span><span style="color:#e6db74">&#34;&#34;</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span><span style="color:#75715e">//参数为空 
</span><span style="color:#75715e"></span>String courseFile <span style="color:#f92672">=</span> directory<span style="color:#f92672">.</span><span style="color:#a6e22e">getCanonicalPath</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span> <span style="color:#f92672">;</span> 
System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span>courseFile<span style="color:#f92672">)</span><span style="color:#f92672">;</span> 
</code></pre></div><p>结果： 
C:\Documents and Settings\Administrator\workspace\projectName 
获取当前类的所在工程路径;</p>
<h6 id="heading2">第三种：</h6>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">URL xmlpath <span style="color:#f92672">=</span> <span style="color:#66d9ef">this</span><span style="color:#f92672">.</span><span style="color:#a6e22e">getClass</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span><span style="color:#f92672">.</span><span style="color:#a6e22e">getClassLoader</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span><span style="color:#f92672">.</span><span style="color:#a6e22e">getResource</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;selected.txt&#34;</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span> 
System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span>xmlpath<span style="color:#f92672">)</span><span style="color:#f92672">;</span> 
</code></pre></div><p>结果： 
file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt 
获取当前工程src目录下selected.txt文件的路径</p>
<h6 id="heading3">第四种：</h6>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span>System<span style="color:#f92672">.</span><span style="color:#a6e22e">getProperty</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;user.dir&#34;</span><span style="color:#f92672">)</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span> 
</code></pre></div><p>结果： 
C:\Documents and Settings\Administrator\workspace\projectName 
获取当前工程路径</p>
<h6 id="heading4">第五种：</h6>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java">System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span> System<span style="color:#f92672">.</span><span style="color:#a6e22e">getProperty</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;java.class.path&#34;</span><span style="color:#f92672">)</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span> 
</code></pre></div><p>结果： 
C:\Documents and Settings\Administrator\workspace\projectName\bin 
获取当前工程路径</p>
<hr>
<h3 id="java1">java获取当前路径的几种方法</h3>
<p>1、利用System.getProperty()函数获取当前路径： 
System.out.println(System.getProperty(&ldquo;user.dir&rdquo;));//user.dir指定了当前的路径</p>
<p>2、使用File提供的函数获取当前路径： 
File directory = new File(&quot;&quot;);//设定为当前文件夹 
try{ 
System.out.println(directory.getCanonicalPath());//获取标准的路径 
System.out.println(directory.getAbsolutePath());//获取绝对路径 
}catch(Exceptin e){}</p>
<p>File.getCanonicalPath()和File.getAbsolutePath()大约只是对于new File(&quot;.&quot;)和new File(&quot;..&quot;)两种路径有所区别。</p>
<p><strong>对于getCanonicalPath()函数，“.&ldquo;就表示当前的文件夹，而”..“则表示当前文件夹的上一级文件夹</strong>  <br>
<strong>对于getAbsolutePath()函数，则不管”.”、“..”，返回当前的路径加上你在new File()时设定的路径</strong>  <br>
<strong>至于getPath()函数，得到的只是你在new File()时设定的路径</strong></p>
<p>比如当前的路径为 C:/test ：<br>
File directory = new File(&ldquo;abc&rdquo;);<br>
directory.getCanonicalPath(); //得到的是C:/test/abc<br>
directory.getAbsolutePath();    //得到的是C:/test/abc<br>
direcotry.getPath();                    //得到的是abc</p>
<p>File directory = new File(&quot;.&quot;);<br>
directory.getCanonicalPath(); //得到的是C:/test<br>
directory.getAbsolutePath();    //得到的是C:/test/.<br>
direcotry.getPath();                    //得到的是.</p>
<p>File directory = new File(&quot;..&quot;); 
directory.getCanonicalPath(); //得到的是C:/<br>
directory.getAbsolutePath();    //得到的是C:/test/..<br>
direcotry.getPath();                    //得到的是..</p>
<p>另外：System.getProperty()中的字符串参数如下：<br>
System.getProperty()参数大全<br>
<strong>java.version          Java Runtime Environment version</strong><br>
<strong>java.vendor          Java Runtime Environment vendor</strong><br>
<strong>java.vendor.url          Java vendor URL</strong><br>
<strong>java.home          Java installation directory</strong><br>
<strong>java.vm.specification.version          Java Virtual Machine specification version</strong>  <br>
<strong>java.vm.specification.vendor          Java Virtual Machine specification vendor</strong>  <br>
<strong>java.vm.specification.name          Java Virtual Machine specification name</strong> <br>
<strong>java.vm.version          Java Virtual Machine implementation version</strong> <br>
<strong>java.vm.vendor          Java Virtual Machine implementation vendor</strong> <br>
<strong>java.vm.name          Java Virtual Machine implementation name</strong> <br>
<strong>java.specification.version          Java Runtime Environment specification version</strong> <br>
<strong>java.specification.vendor          Java Runtime Environment specification vendor</strong> <br>
<strong>java.specification.name          Java Runtime Environment specification name</strong> <br>
<strong>java.class.version          Java class format version number</strong> <br>
<strong>java.class.path          Java class path</strong> <br>
<strong>java.library.path          List of paths to search when loading libraries</strong> <br>
<strong>java.io.tmpdir          Default temp file path</strong> <br>
<strong>java.compiler          Name of JIT compiler to use</strong> <br>
<strong>java.ext.dirs          Path of extension directory or directories</strong> <br>
<strong>os.name          Operating system name</strong> <br>
<strong>os.arch          Operating system architecture</strong> <br>
<strong>os.version          Operating system version</strong> <br>
<strong>file.separator          File separator (&quot;/&rdquo; on UNIX)</strong> <br>
<strong>path.separator          Path separator (&quot;:&rdquo; on UNIX)</strong> <br>
<strong>line.separator          Line separator (&quot;/n&rdquo; on UNIX)</strong> <br>
<strong>user.name          User’s account name</strong> <br>
<strong>user.home          User’s home directory</strong> <br>
<strong>user.dir          User’s current working directory</strong></p>
<h4 id="java2">JAVA中获取路径:</h4>
<h6 id="1jsp">1.jsp中取得路径：</h6>
<p>以工程名为TEST为例：</p>
<p>(1)得到包含工程名的当前页面全路径：request.getRequestURI()<br>
结果：/TEST/test.jsp<br>
(2)得到工程名：request.getContextPath() 
结果：/TEST<br>
(3)得到当前页面所在目录下全名称：request.getServletPath()<br>
结果：如果页面在jsp目录下 /TEST/jsp/test.jsp<br>
(4)得到页面所在服务器的全路径：application.getRealPath(&ldquo;页面.jsp&rdquo;)<br>
结果：D:/resin/webapps/TEST/test.jsp<br>
(5)得到页面所在服务器的绝对路径：absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();<br>
结果：D:/resin/webapps/TEST</p>
<h6 id="2">2.在类中取得路径：</h6>
<p>(1)类的绝对路径：Class.class.getClass().getResource(&quot;/&quot;).getPath()<br>
结果：/D:/TEST/WebRoot/WEB-INF/classes/pack/<br>
(2)得到工程的路径：System.getProperty(&ldquo;user.dir&rdquo;)<br>
结果：D:/TEST</p>
<h6 id="3servlet">3.在Servlet中取得路径：</h6>
<p>(1)得到工程目录：request.getSession().getServletContext().getRealPath(&quot;&quot;) 参数可具体到包名。<br>
结果：E:/Tomcat/webapps/TEST<br>
(2)得到IE地址栏地址：request.getRequestURL()<br>
结果：http://localhost:8080/TEST/test<br>
(3)得到相对地址：request.getRequestURI()<br>
结果：/TEST/test</p>
<hr>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-java" data-lang="java"><span style="color:#66d9ef">public</span>  <span style="color:#66d9ef">void</span>  <span style="color:#a6e22e">TestMain</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span>
	<span style="color:#f92672">{</span>
		Connection conn <span style="color:#f92672">=</span> <span style="color:#66d9ef">null</span><span style="color:#f92672">;</span>
        String sql<span style="color:#f92672">;</span>
        <span style="color:#75715e">// MySQL的JDBC URL编写方式：jdbc:mysql://主机名称：连接端口/数据库的名称?参数=值
</span><span style="color:#75715e"></span>        <span style="color:#75715e">// 避免中文乱码要指定useUnicode和characterEncoding
</span><span style="color:#75715e"></span>        <span style="color:#75715e">// 执行数据库操作之前要在数据库管理系统上创建一个数据库，名字自己定，
</span><span style="color:#75715e"></span>        <span style="color:#75715e">// 下面语句之前就要先创建javademo数据库
</span><span style="color:#75715e"></span>        String url <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;jdbc:mysql://192.168.1.8:13306/jpgk_device?useUnicode=true&amp;characterEncoding=utf8&amp;zeroDateTimeBehavior=convertToNull&#34;</span><span style="color:#f92672">;</span>
 
        <span style="color:#66d9ef">try</span> <span style="color:#f92672">{</span>
            <span style="color:#75715e">// 之所以要使用下面这条语句，是因为要使用MySQL的驱动，所以我们要把它驱动起来，
</span><span style="color:#75715e"></span>            <span style="color:#75715e">// 可以通过Class.forName把它加载进去，也可以通过初始化来驱动起来，下面三种形式都可以
</span><span style="color:#75715e"></span>            Class<span style="color:#f92672">.</span><span style="color:#a6e22e">forName</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;com.mysql.jdbc.Driver&#34;</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span><span style="color:#75715e">// 动态加载mysql驱动
</span><span style="color:#75715e"></span> 
            System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;成功加载MySQL驱动程序&#34;</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span>
            <span style="color:#75715e">// 一个Connection代表一个数据库连接
</span><span style="color:#75715e"></span>            conn <span style="color:#f92672">=</span> DriverManager<span style="color:#f92672">.</span><span style="color:#a6e22e">getConnection</span><span style="color:#f92672">(</span>url<span style="color:#f92672">)</span><span style="color:#f92672">;</span>
            <span style="color:#75715e">// Statement里面带有很多方法，比如executeUpdate可以实现插入，更新和删除等
</span><span style="color:#75715e"></span>            Statement stmt <span style="color:#f92672">=</span> conn<span style="color:#f92672">.</span><span style="color:#a6e22e">createStatement</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span>
            sql <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;create table student(NO char(20),name varchar(20),primary key(NO))&#34;</span><span style="color:#f92672">;</span>
            <span style="color:#66d9ef">int</span> result <span style="color:#f92672">=</span> stmt<span style="color:#f92672">.</span><span style="color:#a6e22e">executeUpdate</span><span style="color:#f92672">(</span>sql<span style="color:#f92672">)</span><span style="color:#f92672">;</span><span style="color:#75715e">// executeUpdate语句会返回一个受影响的行数，如果返回-1就没有成功
</span><span style="color:#75715e"></span>            <span style="color:#66d9ef">if</span> <span style="color:#f92672">(</span>result <span style="color:#f92672">!</span><span style="color:#f92672">=</span> <span style="color:#f92672">-</span>1<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
                System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;创建数据表成功&#34;</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span>
                sql <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;insert into student(NO,name) values(&#39;2012001&#39;,&#39;陶伟基&#39;)&#34;</span><span style="color:#f92672">;</span>
                result <span style="color:#f92672">=</span> stmt<span style="color:#f92672">.</span><span style="color:#a6e22e">executeUpdate</span><span style="color:#f92672">(</span>sql<span style="color:#f92672">)</span><span style="color:#f92672">;</span>
                sql <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;insert into student(NO,name) values(&#39;2012002&#39;,&#39;周小俊&#39;)&#34;</span><span style="color:#f92672">;</span>
                result <span style="color:#f92672">=</span> stmt<span style="color:#f92672">.</span><span style="color:#a6e22e">executeUpdate</span><span style="color:#f92672">(</span>sql<span style="color:#f92672">)</span><span style="color:#f92672">;</span>
                sql <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;select * from student&#34;</span><span style="color:#f92672">;</span>
                ResultSet rs <span style="color:#f92672">=</span> stmt<span style="color:#f92672">.</span><span style="color:#a6e22e">executeQuery</span><span style="color:#f92672">(</span>sql<span style="color:#f92672">)</span><span style="color:#f92672">;</span><span style="color:#75715e">// executeQuery会返回结果的集合，否则返回空值
</span><span style="color:#75715e"></span>                System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;学号\t姓名&#34;</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span>
                <span style="color:#66d9ef">while</span> <span style="color:#f92672">(</span>rs<span style="color:#f92672">.</span><span style="color:#a6e22e">next</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span><span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
                    System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span>
                            <span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span>rs<span style="color:#f92672">.</span><span style="color:#a6e22e">getString</span><span style="color:#f92672">(</span>1<span style="color:#f92672">)</span> <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;\t&#34;</span> <span style="color:#f92672">+</span> rs<span style="color:#f92672">.</span><span style="color:#a6e22e">getString</span><span style="color:#f92672">(</span>2<span style="color:#f92672">)</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span><span style="color:#75715e">// 入如果返回的是int类型可以用getInt()
</span><span style="color:#75715e"></span>                <span style="color:#f92672">}</span>
            <span style="color:#f92672">}</span>
        <span style="color:#f92672">}</span> <span style="color:#66d9ef">catch</span> <span style="color:#f92672">(</span>SQLException e<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
            System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;MySQL操作错误&#34;</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span>
            e<span style="color:#f92672">.</span><span style="color:#a6e22e">printStackTrace</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span>
        <span style="color:#f92672">}</span> <span style="color:#66d9ef">catch</span> <span style="color:#f92672">(</span>Exception e<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
            e<span style="color:#f92672">.</span><span style="color:#a6e22e">printStackTrace</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span>
        <span style="color:#f92672">}</span> <span style="color:#66d9ef">finally</span> <span style="color:#f92672">{</span>
            <span style="color:#66d9ef">try</span> <span style="color:#f92672">{</span>
				conn<span style="color:#f92672">.</span><span style="color:#a6e22e">close</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span>
			<span style="color:#f92672">}</span> <span style="color:#66d9ef">catch</span> <span style="color:#f92672">(</span>SQLException e<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
				<span style="color:#75715e">// TODO Auto-generated catch block
</span><span style="color:#75715e"></span>				e<span style="color:#f92672">.</span><span style="color:#a6e22e">printStackTrace</span><span style="color:#f92672">(</span><span style="color:#f92672">)</span><span style="color:#f92672">;</span>
			<span style="color:#f92672">}</span>
        <span style="color:#f92672">}</span>
	<span style="color:#f92672">}</span>
</code></pre></div>
</div>


  
</article>
      <footer id="main-footer" class="container main_footer">
  

  <div class="container nav foot no-print">
  
<a href="https://lvzongcheng.gitee.io/license">license</a>


  <a class="toplink" href="#">back to top</a>

</div>

  <div class="container credits">
  
<div class="container footline">
  
  code with <!-- raw HTML omitted --><!-- raw HTML omitted -->


</div>


  
<div class="container copyright">
  
  (c) 2015 yourname.


</div>


</div>

</footer>

    </main>
    
<script type="text/javascript">
  (function() {
    
    
    if (window.location.hostname == "localhost")
      return;
    var dsq = document.createElement('script'); dsq.async = true; dsq.type = 'text/javascript';
    dsq.src = '//your_disqus_shortname.disqus.com/count.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })();
</script>



<script src="/js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>



    
  </body>
</html>

